import {
	http,
	Method
} from "@/utils/request.js";

import api from "@/config/api.js";


/**
 * 获取订单列表
 * @param 
 */
export function getOrderList(params) {
	return http.request({
		url: '/product-orders/userOrderHistory',
		method: Method.GET,
		data: params
	});
}
/**
 * 获取接的服务订单列表
 * @param 
 */
export function getServiceOrderList(params) {
	return http.request({
		url: '/orders',
		method: Method.GET,
		data: params
	});
}



/**
 * 获取发布的服务订单列表
 * @param 
 */
export function getMyServiceOrderList(params) {
	return http.request({
		url: '/service/order/my/list',
		method: Method.GET,
		params
	});
}

/**
 * 获取发布服务订单
 * @param 
 */
export function sendServiceOrder(params) {
	return http.request({
		url: '/orders',
		method: Method.POST,
		data: params
	});
}


/**
 * 接单
 * @param 
 */
export function acceptOrder(id) {
	return http.request({
		url: `/orders/accept`,
		method: Method.POST,
		data: {
			orderId: id
		}
	});
}
/**
 * 取件
 * @param 
 */
export function orderPick(data) {
	return http.request({
		url: `/orders/pick`,
		method: Method.POST,
		data
	});
}
/**
 * 送达
 * @param 
 */
export function orderPicked(data) {
	return http.request({
		url: `/orders/deliver`,
		method: Method.POST,
		data
	});
}

/**
 * 确认收货
 * @param 
 */
export function orderComplete(data) {
	return http.request({
		url: `/orders/complete`,
		method: Method.POST,
		data
	});
}
/**
 * 取消订单
 * @param 
 */
export function orderCancel(data) {
	return http.request({
		url: `/orders/cancel`,
		method: Method.POST,
		data
	});
}

/**
 * 获取服务订单详情
 * @param 
 */
export function serviceOrderDetail(id) {
	return http.request({
		url: `/orders/${id}`,
		method: Method.GET,
	});
}

/**
 * 获取抢到的服务订单
 * @param 
 */

export function myServiceOrder(params) {
	return http.request({
		url: `/orders/userAcceptedOrders`,
		method: Method.GET,
		params: params
	});
}

/**
 * 历史发布服务订单
 * @param 
 */


export function orderTransform(params) {
	return http.request({
		url: `/orders/userOrderHistory`,
		method: Method.GET,
		data: params
	});
}

/**
 * 取消订单
 * @param 
 */


export function cancelOrder(params) {
	return http.request({
		url: `/orders/cancel`,
		method: Method.POST,
		data: params
	});
}

export function orderEvaluate(params) {
	return http.request({
		url: `/orders/comment`,
		method: Method.POST,
		data: params
	});
}


/**
 * 获取商品详情
 * @param 
 */
export function orderInfo(id) {
	return http.request({
		url: `/products/${id}`,
		method: Method.GET,
	});
}

/**
 * 获取家庭服务类型列表
 * @param {string} category 'cleaning' 或 'repair'
 * @param {string} communityId 可选，指定小区ID
 */
export function getRepairServiceTypes(category, communityId) {
	let url = '/repair-service/types';
	const params = [];
	
	if (category) {
		params.push(`category=${category}`);
	}
	if (communityId) {
		params.push(`communityId=${communityId}`);
	}
	
	if (params.length > 0) {
		url += `?${params.join('&')}`;
	}
	
	return http.request({
		url: url,
		method: Method.GET
	});
}

/**
 * 家庭服务预约下单
 * @param {object} params { typeId, dateTime, address, remark, communityId }
 */
export function createRepairServiceOrder(params) {
	return http.request({
		url: '/repair-service/order',
		method: Method.POST,
		data: params
	});
}

/**
 * 获取家庭服务历史订单
 * @param {object} params { category, status, page, limit }
 */
export function getRepairServiceOrderHistory(params = {}) {
	return http.request({
		url: '/repair-service/order/history',
		method: Method.GET,
		data: params
	});
}

/**
 * 取消家庭服务订单
 * @param {string} orderId 订单id
 * @param {string} reason 取消原因
 */
export function cancelRepairServiceOrder(orderId, reason) {
	return http.request({
		url: `/repair-service/order/${orderId}/cancel`,
		method: Method.POST,
		data: { reason }
	});
}

/**
 * 评价家庭服务订单
 * @param {string} orderId 订单id
 * @param {object} data { rating, comment }
 */
export function commentRepairServiceOrder(orderId, data) {
	return http.request({
		url: `/repair-service/order/${orderId}/comment`,
		method: Method.POST,
		data
	});
}

/**
 * 创建家庭服务订单并支付
 * @param {object} params { typeId, dateTime, address, remark, payType, openid, communityId }
 */
export function createRepairServiceOrderWithPayment(params) {
	return http.request({
		url: '/repair-service/order',
		method: Method.POST,
		data: params
	});
}

/**
 * 查询家庭服务订单支付状态
 * @param {string} orderId 订单ID
 */
export function getRepairServiceOrderPaymentStatus(orderId) {
	return http.request({
		url: `/repair-service/order/${orderId}/payment-status`,
		method: Method.GET
	});
}

/**
 * 申请家庭服务订单退款
 * @param {string} orderId 订单ID
 * @param {string} reason 退款原因
 */
export function refundRepairServiceOrder(orderId, reason) {
	return http.request({
		url: `/repair-service/order/${orderId}/refund`,
		method: Method.POST,
		data: { reason }
	});
}

/**
 * 获取家庭服务订单详情
 * @param {string} orderId 订单ID
 */
export function getRepairServiceOrderDetail(orderId) {
	return http.request({
		url: `/repair-service/order/${orderId}`,
		method: Method.GET
	});
}

/**
 * 重新调取支付
 * @param {string} id 订单id
 * @param {object} data { rating, comment }
 */
export function rePay(data) {
	return http.request({
		url: `/orders/rePay`,
		method: Method.POST,
		data
	});
}

/**
 * 查询支付状态
 * @param {string} id 订单id
 * @param {object} data { rating, comment }
 */
export function getOrderPayStatus(data) {
	return http.request({
		url: `/orders/paySuccessNotify`,
		method: Method.POST,
		data
	});
}